package win.ganbo.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.ganbo.ssm.entity.User;
import win.ganbo.ssm.service.IUserService;
import win.ganbo.ssm.utils.Result;
import win.ganbo.ssm.vo.UserVO;
import win.ganbo.ssm.vo.test.AddressGroup;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @Author ganbo E-mail:bo.gan@56qq.com
 * @CreateTime 2017-08-01 20:55
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    public List<UserVO> list() {
        List<User> users = userService.selectAll();
        return userTOuserVo(users);
    }

    public UserController() {
        System.out.println("controller被初始化========================");
    }

    @PostMapping("/add")
    public Result add(AddressGroup addressGroup) {
        System.out.println("addressGroup=================>" + addressGroup);
        return new Result();
    }

    private List<UserVO> userTOuserVo(List<User> list) {
        List<UserVO> volist = new ArrayList<UserVO>();
        for (User user : list) {
            UserVO vo = new UserVO();
            vo.setId(user.getId());
            vo.setUsername(user.getUsername());
            vo.setBirthday(user.getBirthday());
            vo.setSex(user.getSex());
            vo.setAddress(user.getAddress());
            vo.setUsertype(user.getUsertype());
            vo.setActivate(user.getActivate().getName());
            volist.add(vo);
        }
        return volist;
    }
}
